<%@tag import="java.util.Set"%>
<%@tag import="by.epam.communalservices.utils.Login"%>
<%@tag import="java.sql.Connection"%>
<%@tag import="by.epam.communalservices.dao.jdbc.JdbcDaoUser"%>
<%@tag import="by.epam.communalservices.entity.User"%>
<%@tag import="java.util.List"%>

<%@ attribute name="name" required="true"%>
<%@ attribute name="selected" required="true"%>
<%@ attribute name="roleMarker" %>
<%@ attribute name="attr"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<select name="${name}" ${attr}>
	<OPTION VALUE='0' ${selected==0? 'SELECTED' : ''}><c:out value="не задан"/></OPTION>
	<%
		Long selected = 0L;
	    try {
	        selected = Long.parseLong(getJspContext().getAttribute("selected").toString());
	    } catch (NumberFormatException e) {
	    }
        JdbcDaoUser userDao = new JdbcDaoUser((Connection) request.getSession().getAttribute("currentConnection"));
        List<User> userList = userDao.findAll();
        
        if (getJspContext().getAttribute("roleMarker")!=null) {
            for (User u : userList) {
                Connection con = (Connection) request.getSession().getAttribute("currentConnection");
                Login login = new Login(con);
                Set<String> r = login.getRole(u.getId());
                if (r.contains(getJspContext().getAttribute("roleMarker").toString())) {
                    u.setLogin(u.getLogin()+" ("+getJspContext().getAttribute("roleMarker").toString()+")");
                }
            }
        }
	        

        for (User item : userList) {
            out.println("<OPTION VALUE='" + item.getId() + "' " + (item.getId() == selected ? "SELECTED" : "") + ">"
                    + item.getLogin() + "</OPTION>");
        }
	%>

</select>
